﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CuteEditor;
using Wjtour.Domain;
using Wjtour.BLL;
using Hjp.FrameWork.Common;
using System.IO;

namespace Wjtour.Web.Admin.scene
{
    public partial class resturantaddedi : BasePage
    {
        protected TextBox scenename;
        protected HiddenField sceneid;
        protected TextBox img;
        protected FileUpload upload;
        protected Button btnPublish;
        protected Image imgPreview;
        protected Button submit;
        protected TextBox resturantname;
        protected TextBox address;
        protected TextBox opentime;
        protected TextBox contactphone;
        protected TextBox holdmans;
        protected TextBox averagecosume;
        protected TextBox trafficguide;
        protected Editor introduce;

        protected int id = 0;
        protected int resturantid = 0;
        protected int sid = 0;
        Wjtour.Domain.sceneresturant sceneresturant = new Domain.sceneresturant();
        Wjtour.Domain.scene scene = new Domain.scene();
        Wjtour.Domain.resturant resturant = new Wjtour.Domain.resturant();
        sceneresturant_bll _bll = new sceneresturant_bll();
        scene_bll _sbll = new scene_bll();
        resturant_bll _resbll = new resturant_bll();

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Utils.StrToInt(Request["id"], 0);
            submit.Click += new EventHandler(submit_Click);
            btnPublish.Click += btnPublish_Click;
            if (id > 0)
            {
                sceneresturant = _bll.Load(id);
                scene = _sbll.Load(sceneresturant.sceneid);
                resturant = _resbll.Load(sceneresturant.resturantid);
                sid = sceneresturant.sceneid;
                resturantid = sceneresturant.resturantid;

                if (!IsPostBack)
                {
                    if (sceneresturant != null)
                    {
                        scenename.Text = scene.scenename;
                        sceneid.Value = sceneresturant.sceneid.ToString();
                        img.Text = resturant.img;
                        imgPreview.ImageUrl = resturant.img;
                        resturantname.Text = resturant.resturantname;
                        address.Text = resturant.address;
                        opentime.Text = resturant.opentime;
                        contactphone.Text = resturant.contactphone;
                        holdmans.Text = resturant.holdmans;
                        averagecosume.Text = resturant.averagecosume;
                        trafficguide.Text = resturant.trafficguide;
                        introduce.Text = resturant.introduce;
                    }
                }
            }
        }

        void btnPublish_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(upload.FileName))
            {
                Utils.Alert(this.Page, "请选择要上传的文件！");
                return;
            }
            string fileExt = Path.GetExtension(upload.FileName);
            List<string> allowExt = new List<string>();
            allowExt.Add(".jpg");
            allowExt.Add(".png");
            allowExt.Add(".gif");

            if (!allowExt.Contains(fileExt))
            {
                Utils.Alert(this.Page, "系统只允许上传jpg,jpeg,gif的文件！");
                return;
            }
            string uploadDir = Server.MapPath("/uploads");
            if (!Directory.Exists(uploadDir))
            {
                Directory.CreateDirectory(uploadDir);
            }

            string guid = GetFileName("time");
            string filename = guid + fileExt;
            string path = uploadDir + "\\" + filename;
            if (System.IO.File.Exists(filename)) System.IO.File.Delete(path);

            try
            {
                upload.SaveAs(path);
                //string newpath = ImageUtil.ConvertImageToProperSize(HttpContext.Current, "/uploads/" + filename, 100, 100, "jpg");
                imgPreview.ImageUrl = "/uploads/" + filename;
                img.Text = "/uploads/" + filename;
            }
            catch (Exception ex)
            {
                Utils.Alert(this.Page, "文件文件出错，文件大小超过限制或网络原因！");
                return;
            }
        }

        void submit_Click(object sender, EventArgs e)
        {
            sid = Utils.StrToInt(sceneid.Value, 0);
            if (sid == 0)
            {
                Utils.Alert(this.Page, "请选择景点！");
                return;
            }
            resturant.id = resturantid;
            resturant.img = img.Text;
            resturant.resturantname = resturantname.Text;
            resturant.address = address.Text;
            resturant.opentime = opentime.Text;
            resturant.contactphone = contactphone.Text;
            resturant.holdmans = holdmans.Text;
            resturant.averagecosume = averagecosume.Text;
            resturant.trafficguide = trafficguide.Text;
            resturant.introduce = introduce.Text;
            if (resturant.id == 0)
                resturant.addtime = DateTime.Now;

            bool b = _bll.Save(resturant, sid);
            if (b)
            {
                Utils.Alert(this.Page, "保存成功！");
                Utils.Redirect(this.Page, "resturantlist.aspx", false);
            }
        }
    }
}